//
//  YDStatusViewModel.swift
//  YDWeibo01
//
//  Created by suibian on 16/8/13.
//  Copyright © 2016年 iitcast. All rights reserved.
//

import UIKit

class YDStatusViewModel: NSObject {

//    创建一个对象model
    var statusModel:YDStatusModel?
//    创建微博等级图片
    var mbrankImage:UIImage?
//    微博认证图片
    var verifiedImage:UIImage?
//    微博来源
    var sinaSourceStr:String?
//    转发 评论 赞
    var repostsCountStr:String?
    var commentCountStr:String?
    var attitudesCountStr:String?
//    原创微博的微博正文副文本
    var orginalAttr:NSMutableAttributedString?
//    转发微博的正文副文本
    var retweetAttr:NSMutableAttributedString?
    
//    微博时间
    var sinaTimeStr:String?{
   return NSDate.getSinaDate(statusModel?.created_at ?? "").sinaDateDescription
    }
//    重载构造函数创见模型
    init(statusModel:YDStatusModel) {
//        如果是必选值必须写在super前
        super.init()
        self.statusModel = statusModel
        mbrankImage = dealMbrankImage(statusModel.user?.mbrank ?? 0)
        verifiedImage = dealVerifiedImage(statusModel.user?.verified ?? -1)
//        init方法中给来源字符串赋值
        sinaSourceStr = dealSinaSource(statusModel.source ?? "")
//        转发 评论 赞
         repostsCountStr =  getSinaCountStr(statusModel.reposts_count ?? 0, title: "转发")
         commentCountStr =  getSinaCountStr(statusModel.comments_count ?? 0, title: "评论")
         attitudesCountStr =  getSinaCountStr(statusModel.attitudes_count ?? 0, title: "赞")
        orginalAttr = dealSinaContentAttr(statusModel.text ?? "")
        retweetAttr = dealSinaContentAttr(statusModel.retweeted_status?.text ?? "")
    }
    
}





//处理微博评论 赞逻辑
extension YDStatusViewModel{

    func getSinaCountStr(count:Int,title:String) -> String{
        if count <= 0 {
            return title
            
        }else if count > 0 && count < 10000{
            
            return "\(count)"
            
        }else{
            
            let cFloat = CGFloat(count/1000)/10
            var cFloatStr = "\(cFloat)万"
            //            判断是否包含.0
            if cFloatStr.containsString(".0"){
                //            替换字符串
                cFloatStr = cFloatStr.stringByReplacingOccurrencesOfString(".0", withString:"")
            }
            
            return cFloatStr
            
        }
        
        
    }
}


//处理微博来源信息
extension YDStatusViewModel{
    //    处理来源信息
    func dealSinaSource(source:String) ->String{
        //        判断字符串是否包含特殊字符
        if source.containsString("\">"){
            //            头位置
            let startRange = source.rangeOfString("\">")!
            
            //            尾位置
            let endRange = source.rangeOfString("</")!
            
            let result = source.substringWithRange(startRange.endIndex..<endRange.startIndex)
            return result
        }
        //    如果为空字符串
        return "来自哪里好呢"
    }

}

//处理cell中的图片的数据逻辑
extension YDStatusViewModel{
    //    处理微博等级图片 url提供的是int 而给的是image 需要返回一个image图片
    
    func dealMbrankImage(mbrank:Int) ->UIImage? {
        
        if mbrank > 0 && mbrank < 7{
            return UIImage(named: "common_icon_membership_level\(mbrank)")
            
        }
        return UIImage(named: "common_icon_membership")
    }
    //处理微博用户认证
    func dealVerifiedImage(verified:Int) -> UIImage? {
        switch verified{
            
        case 1:
            return UIImage(named: "avatar_vip")
        case 2,3,5:
            return UIImage(named: "avatar_enterprise_vip")
        case 220:
            return UIImage(named: "avatar_grassroot")
        default:
            return UIImage(named: "avatar_vgirl")
        }
    }
}
extension YDStatusViewModel{
    //    处理微博正文副文本显示 (当前是文字显示)
    func dealSinaContentAttr(text:String)->NSMutableAttributedString{
        /**
         *  正则匹配的规则
         *
         *  @param NSString 正则匹配的规则
         - 匹配的个数
         - 匹配的结果
         - 匹配的范围
         - 匹配是否停止
         *
         *  @return
         */
         //        创建一个可变数组 保存拼接的模型字符串
        var matchArray:[YDMatchModel] = [YDMatchModel]()
        //把当前的字符串转换为副文本
        let allTextAttr = NSMutableAttributedString(string: text)
        let regex = "\\[[a-zA-Z0-9\\u4e00-\\u9fa5]+\\]"
        (text as NSString).enumerateStringsMatchedByRegex(regex) { (count, result, range, _) -> Void in
            //            实例化model
            let model = YDMatchModel(string:(result.memory as! String) , range: range.memory)
            //            添加模型到数组中
            matchArray.append(model)
            //            print(result.memory,range.memory)
            
            
        }
        //        反向遍历数组
        for matchModel in matchArray.reverse(){
            guard let emoticonModel = YDEmoticonTools.shareTools.searchEmoticonInDefaultEmoticonsOrLxhEmoticons((matchModel.string))else{
                //                防止跳出
                continue
            }
        
            //            创建一个不可变的副文本
            let attr = NSAttributedString.emoticonAttributedString(emoticonModel, textFont: 15, y: -4)
            //            替换
            allTextAttr.replaceCharactersInRange(matchModel.range, withAttributedString: attr)
        }
        return allTextAttr
    }




}



